package com.question.dao.impl;

import java.sql.SQLException;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import com.question.dao.ISeedtimeDao;
import com.question.entity.User;
import com.question.utils.DataSourceUtils;

public class SeedtimeDaoImpl implements ISeedtimeDao{

	@Override
	public Long existUserByUsername(String username) throws SQLException {
		QueryRunner qr = new QueryRunner(DataSourceUtils.getDatasource());
		String sql = "select count(*) from t_user where username = ?";
		return qr.query(sql, new ScalarHandler<>(), username);
	}

	@Override
	public void signup(User user) throws SQLException {
		QueryRunner qr = new QueryRunner(DataSourceUtils.getDatasource());
		String sql = "insert into t_user (username,password,display_name)values(?,?,?)";
		qr.update(sql, user.getUsername(),user.getPassword(),user.getDisplay_name());
	}

	@Override
	public User login(User user) throws SQLException {
		QueryRunner qr = new QueryRunner(DataSourceUtils.getDatasource());
		String sql = "select * from t_user where username = ? and password = ?";
		return qr.query(sql, new BeanHandler<User>(User.class),user.getUsername(),user.getPassword());
	}

}
